package com.lagou.edu.service.impl;

import com.lagou.edu.dao.TokenDao;
import com.lagou.edu.pojo.Token;
import com.lagou.edu.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class TokenServiceImpl implements TokenService {

    @Autowired
    private TokenDao tokenDao;

    @Override
    public Long save(Token token) {
        Token unique = this.getUnique(new Token(token.getEmail()));
        if (unique == null) {
            Token token1 = tokenDao.save(token);
            return token1.getId();
        } else {
            return 0L;
        }
    }

    @Override
    public Token getUnique(Token token) {
        Example<Token> example = Example.of(token);
        Optional<Token> one = tokenDao.findOne(example);
        return one.orElse(null);
    }

}
